package com.cncsi.service;


import com.cncsi.dao.OrgRepositoty;
import com.cncsi.entity.Org;
import com.cncsi.entity.zTree;
import com.cncsi.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 部门管理service
 * 2018年9月11日, AM 08:52:14
 *
 * @author 姜文
 */
@Service
@Transactional
public class OrgService {

    @Autowired
    private OrgRepositoty orgRepositoty;
    @Autowired
    private QueryUtil queryUtil;

    /**
     * 组织机构树
     * 2018年9月11日, AM 08:52:14
     *
     * @author 姜文
     */
    public List<zTree> OrgTree() {
        String sql = "SELECT org_id id, org_name name, parent_org_id pId FROM org WHERE sts= '1'";
        return queryUtil.query(sql, zTree.class);
    }

    /**
     * 查询
     * 2018年9月11日, AM 08:52:14
     *
     * @author 姜文
     */
    public Org FindOneOrg(String orgId) {
        String sql = "SELECT * FROM `org` WHERE sts = '1' AND org_id = '"+orgId+"'";
        List<Org> orgList = queryUtil.query(sql, Org.class);
        return orgList.get(0);
    }

    /**
     * 删除
     * 2018年9月11日, PM 01:23:12
     *
     * @author 姜文
     */
    public String deleteOrg(String orgId) {
        String pSql = "SELECT * FROM `org` WHERE sts = '1' AND parent_org_id = '"+orgId+"'";
        List<Org> parentOrgList = queryUtil.query(pSql, Org.class);
        if(parentOrgList.size() > 0){
            return "该部门有下属子部门,请先删除子部门";
        }else{
            try{
                String sql = "SELECT * FROM `org` WHERE sts = '1' AND org_id = '"+orgId+"'";
                Org org = queryUtil.query(sql, Org.class).get(0);
                org.setSts(0);
                orgRepositoty.saveAndFlush(org);
                return "删除成功";
            }catch(Exception e){
                System.out.println(e);
                return "删除失败";
            }
        }
    }

    /**
     * 保存
     * 2018年9月11日, PM 02:35:41
     *
     * @author 姜文
     */
    public String saveOrg(Org org) {
        try{
            if(org.getParentOrgId() == null || "".equals(org.getParentOrgId())){
                org.setOrgFlag(1);
                org.setCdate(new Date());
                org.setSts(1);
            }else{
                Org parentOrg = this.FindOneOrg(org.getParentOrgId());
                org.setOrgFlag(parentOrg.getOrgFlag()+1);
                org.setCdate(new Date());
                org.setSts(1);
            }
            orgRepositoty.save(org);
            return "新增成功";
        }catch(Exception e){
            System.out.println(e);
            return "新增失败";
        }
    }

    /**
     * 保存
     * 2018年9月11日, PM 02:35:41
     *
     * @author 姜文
     */
    public String updateOrg(Org org) {
        try{
            Org orgNew = this.FindOneOrg(org.getOrgId());
            org.setOrgFlag(orgNew.getOrgFlag());
            org.setCdate(orgNew.getCdate());
            org.setSts(orgNew.getSts());
            orgRepositoty.saveAndFlush(org);
            return "修改成功";
        }catch(Exception e){
            System.out.println(e);
            return "修改失败";
        }
    }


}
